package cn.chuanw.db.service;

import cn.chuanw.db.dao.WIncomerentRecordMapper;
import cn.chuanw.db.dao.WRoomInfoMapper;
import cn.chuanw.db.domain.WIncomerentRecord;
import cn.chuanw.db.domain.WRoomInfo;
import com.github.pagehelper.PageHelper;
import cn.chuanw.db.domain.WIncomerentRecordExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: wangkun
 * @Date: 20-02-09 下午 4:02
 * @Description: 租户交租类
 **/
@Service
public class IncomeRentRecordService {
    @Resource
    private WIncomerentRecordMapper wIncomerentRecordMapper;

    @Resource
    private WRoomInfoMapper wRoomInfoMapper;

    public List<WIncomerentRecord> querySelective(String tenantId, String houseId, String roomId, LocalDate payDate, String userId, Integer incomeStatus, Integer page, Integer size) {
        WIncomerentRecordExample wIncomerentRecordExample = new WIncomerentRecordExample();
        WIncomerentRecordExample.Criteria criteria = wIncomerentRecordExample.createCriteria();
        if (!StringUtils.isEmpty(tenantId)) {
            criteria.andTenantIdEqualTo(tenantId);
        }
        if (!StringUtils.isEmpty(houseId)) {
            criteria.andHouseIdEqualTo(houseId);
        }
        if (!StringUtils.isEmpty(roomId)) {
            criteria.andRoomIdEqualTo(roomId);
        }
        if (!StringUtils.isEmpty(payDate)) {
            criteria.andIncomeTimeEqualTo(payDate);
        }
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(incomeStatus)) {
            criteria.andIncomeStatusEqualTo(incomeStatus);
        }
        criteria.andDelFlagEqualTo(0);
        wIncomerentRecordExample.setOrderByClause("update_time  desc");
        PageHelper.startPage(page, size);
        return wIncomerentRecordMapper.selectByExample(wIncomerentRecordExample);
    }

    public void create(WIncomerentRecord wIncomerentRecord) {
        wIncomerentRecord.setIncomeTime(LocalDate.now());
        wIncomerentRecord.setUpdateTime(LocalDateTime.now());
        String roomId = wIncomerentRecord.getRoomId();
        WRoomInfo wRoomInfo = wRoomInfoMapper.selectByPrimaryKey(roomId);
        wIncomerentRecord.setHouseId(wRoomInfo.getHouseId());
        wIncomerentRecordMapper.insertSelective(wIncomerentRecord);
    }

    public int updateById(WIncomerentRecord wIncomerentRecord) {
        wIncomerentRecord.setUpdateTime(LocalDateTime.now());
        String roomId = wIncomerentRecord.getRoomId();
        WRoomInfo wRoomInfo = wRoomInfoMapper.selectByPrimaryKey(roomId);
        wIncomerentRecord.setHouseId(wRoomInfo.getHouseId());
        return wIncomerentRecordMapper.updateByPrimaryKeySelective(wIncomerentRecord);
    }

    public void archiveIncomeRent(String ids) {
        if (!StringUtils.isEmpty(ids)) {
            String[] split = ids.split(",");
            for (String s : split) {
                WIncomerentRecord wIncomerentRecord = new WIncomerentRecord();
                wIncomerentRecord.setId(s);
                wIncomerentRecord.setIncomeStatus(3);
                wIncomerentRecord.setArchiveTime(LocalDateTime.now());
                wIncomerentRecordMapper.updateByPrimaryKeySelective(wIncomerentRecord);
            }
        }
    }


    /**
     * @Auther: wangkun
     * @Date:  20-03-24 上午 9:46
     * @Description: 计算当前收租金额，归档收租金额
     **/
    public Object tatolIncomeRent(){
        Integer nowMoney = wIncomerentRecordMapper.sumMoney(1);//当前
        Integer archiveMoney = wIncomerentRecordMapper.sumMoney(3); //归档
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("nowMoney",nowMoney==null?0:nowMoney);
        data.put("archiveMoney",archiveMoney==null?0:archiveMoney);
        return data;
    }

}
